################################################################################
# Replication code for 
# Esol Cho, "Domestic Groups' Testimonies at US Foreign Aid Hearings from 1980 
# to 2020: Findings from a New Dataset," 
# Foreign Policy Analysis.
# Analysis conducted in R 4.2.2 on MacOS 14.5
################################################################################

library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)
library(cowplot)

dat <- read.csv("corpus.csv", header = T, stringsAsFactors = F, sep=",")
head(dat,1)

################################################################################
# Figure 1: Testimonies by group type at Congressional hearings for foreign aid.
################################################################################

fig1.1 <-
  dat %>% group_by(type) %>% count() %>% 
  ungroup() %>%
  mutate(percent = n/sum(n)*100 )%>% as.data.frame() 
group.colors <- c("Firm" = "#E3DB71", "NGO" = "deepskyblue4", "Expert"=  "grey60")
f1 <- ggplot(fig1.1, aes(x=type, y=percent,fill=type )) + 
  geom_bar(position="dodge", stat="identity",width = 0.35) +
  scale_fill_manual(values=group.colors) +
  labs(x=NULL,y="Percent of Testimonies") + theme_classic() +
  theme( legend.title = element_blank(), legend.position = 'none',
         plot.title = element_text(hjust = 0.5, face = 'plain')) +
  ggtitle('Testimonies by Group Type, %') +
  geom_text(aes(label = scales::percent(percent*0.01)), 
            vjust = -0.5, size = 3, position = position_dodge(0.35))

fig1.2 <-
  dat %>% mutate(Is_Divided_Gov = ifelse(president_party==house_control, 
                                         "Unified Government",  
                                         "Divided Government")) %>%
  mutate(Is_Divided_Gov = stringr::str_trim(str_replace(str_trim(Is_Divided_Gov),
                                                        "Government", '')) ) %>%
  mutate(house_control = stringr::str_trim(str_replace(str_trim(house_control),
                                                       "Democratic", 'D')) ) %>%
  mutate(house_control = stringr::str_trim(str_replace(str_trim(house_control),
                                                       "Republican", 'R')) ) %>%
  unite(grouping, Is_Divided_Gov,house_control, sep = "-"  )  %>%
  group_by(grouping,type) %>% count() %>%  
  ungroup() %>% group_by(grouping) %>%
  mutate(percent = n/sum(n)*100 )%>% as.data.frame() 

f2 <- ggplot(fig1.2, aes(x=grouping, y=percent,fill=type )) + 
  geom_bar(position="dodge", stat="identity",width = 0.35) +
  scale_fill_manual(values=group.colors) +
  labs(x=NULL,y="Percent of Testimonies") + theme_classic() +
  theme( legend.title = element_blank(), legend.position = 'top', 
         legend.justification = c(1,1) ,
         plot.title = element_text(hjust = 0.5, face = 'plain')) +
  ggtitle('Divided vs. Unified Government - By Majority Party in the House') +
  geom_text(aes(label = scales::percent(percent*0.01)), vjust = -0.5, size = 3, 
            position = position_dodge(0.35))

cowplot::plot_grid(f1,f2, nrow=1)

################################################################################
# Figure 2: Domestic groups testifying before foreign aid hearings, 1980-2020. 
################################################################################

fig2.1 <- 
  dat %>% group_by(type) %>% 
  count(year) %>% ungroup() %>%  as.data.frame()

group.colors <- c("Firm" = "#E3DB71", "NGO" = "deepskyblue4", "Expert"= "grey60")
breaks <- scales::extended_breaks(n = 10)(range(fig2.1$year))
p1 <- 
  ggplot(fig2.1, aes(as.factor(year), n, group=type, colour=type)) + 
  geom_line(linewidth=1.1)+ labs(x=NULL,y="Number of Testimonies") + 
  theme_classic()+
  ggtitle('Testimonies') +
  theme( legend.title = element_blank(), legend.position = 'top', 
         legend.justification = c(1,1) ,
         plot.title = element_text(hjust = 0.5, face = 'plain')) +
  scale_x_discrete(breaks = breaks)+
  scale_color_manual(values=group.colors)

fig2.2 <- dat %>% group_by(year) %>% count(citation) %>%
  ungroup() %>% dplyr::select(-c(n)) %>% 
  group_by(year) %>% count() %>% rename(Hearings=n) %>% as.data.frame()
breaks <- scales::extended_breaks(n = 10)(range(fig2.2$year))
p2 <- ggplot(fig2.2, aes(year, Hearings)) + 
  geom_line( linewidth=1.1, color="#117733")+ 
  labs(x=NULL,y="Number of Hearings") + theme_classic()+
  ggtitle('Hearings') +
  theme( legend.title = element_blank(), 
         plot.title = element_text(hjust = 0.5, face = 'plain')) +
  scale_x_continuous(breaks = breaks) 

cowplot::plot_grid(p1,p2, nrow=1)

################################################################################
# Table 1: Five most common committees for each group type.
################################################################################

dat %>%
  group_by(type) %>%                                  
  count(committee) %>%                              
  mutate(total_n = sum(n)) %>%                     
  slice_max(order_by = n, n = 5) %>%                
  arrange(desc(total_n), desc(n)) %>%             
  select(-total_n) %>% as.data.frame()     
